home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap2 / 2_8 / chkagent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  1.5 KB  |  68 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* function prototype */
  6. int CheckBrowser();
  7.  
  8. main()
  9. {
  10.     /* Send mime type */
  11.     printf("Content-type: text/html\n\n");
  12.  
  13.     /* Start HTML output */
  14.     printf("<HTML><HEAD>\n");
  15.     printf("<TITLE>CGI Script How-To: Test Script</TITLE>\n");
  16.     printf("</HEAD><BODY>\n");
  17.  
  18.     if (CheckBrowser() == 0)
  19.     {
  20.       /* Unidentified browser, so display an inline GIF image */
  21.       printf("<IMG SRC=\"/images/fish33.gif\">\n");
  22.     }
  23.  
  24.     printf("</BODY></HTML>\n");
  25.     exit(0);
  26. }
  27.  
  28.  
  29. /* function CheckBrowser
  30.  *
  31.  * Parses the HTTP_USER_AGENT environment variable and outputs a different
  32.  * output file to a Mozilla or Lynx Web client browser.
  33.  *
  34.  * Returns: 1 if target browser found and output sent,
  35.  *          0 if not found - meaning nothing was done.
  36.  */
  37.  
  38. int CheckBrowser()
  39. {
  40.   char* user_agent = getenv("HTTP_USER_AGENT");
  41.  
  42.   if (user_agent == NULL)
  43.     {
  44.       return 0;                /* agent not defined -> not defined */
  45.     }
  46.  
  47.   printf("<H2>Browser = %s</H2><P>\n", user_agent);
  48.  
  49.   if (strncmp(user_agent, "Mozilla", 7) == 0)
  50.     {
  51.       /* Display a JPEG image for Netscape browsers */
  52.       printf("<IMG SRC=\"/images/fish33.jpg\">\n");
  53.       return 1;                /* agent identified -> Netscape */
  54.     }
  55.   else if (strncmp(user_agent, "Lynx", 4) == 0)
  56.     {
  57.       /* For a text browser show a hyperlink to an image to download it */
  58.       printf("Try this <A HREF=\"/images/fish33.jpg\">link</a> to download an         image.\n");
  59.       return 1;                /* agent identified -> Lynx */
  60.     }
  61.   
  62.   return 0;                /* agent not defined -> other */
  63. }
  64.  
  65. /*
  66.  * end of chkagent.c
  67.  */
  68.